<!doctype html>

<html>
<head>
  <link rel="shortcut icon" href="static/images/favicon.ico" type="image/x-icon">
  <title>jsxmlhttpdatasource.js (Closure Library API Documentation - JavaScript)</title>
  <link rel="stylesheet" href="static/css/base.css">
  <link rel="stylesheet" href="static/css/doc.css">
  <link rel="stylesheet" href="static/css/sidetree.css">
  <link rel="stylesheet" href="static/css/prettify.css">

  <script>
     var _staticFilePath = "static/";
  </script>

  <script src="static/js/doc.js">
  </script>

  <meta charset="utf8">
</head>

<body onload="prettyPrint()">

<div id="header">
  <div class="g-section g-tpl-50-50 g-split">
    <div class="g-unit g-first">
      <a id="logo" href="index.html">Closure Library API Documentation</a>
    </div>

    <div class="g-unit">
      <div class="g-c">
        <strong>Go to class or file:</strong>
        <input type="text" id="ac">
      </div>
    </div>
  </div>
</div>

<div class="clear"></div>

<h2><a href="closure_goog_datasource_jsxmlhttpdatasource.js.html">jsxmlhttpdatasource.js</a></h2>

<pre class="prettyprint lang-js">
<a name="line1"></a>// Licensed under the Apache License, Version 2.0 (the &quot;License&quot;);
<a name="line2"></a>// you may not use this file except in compliance with the License.
<a name="line3"></a>// You may obtain a copy of the License at
<a name="line4"></a>//
<a name="line5"></a>//     http://www.apache.org/licenses/LICENSE-2.0
<a name="line6"></a>//
<a name="line7"></a>// Unless required by applicable law or agreed to in writing, software
<a name="line8"></a>// distributed under the License is distributed on an &quot;AS IS&quot; BASIS,
<a name="line9"></a>// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
<a name="line10"></a>// See the License for the specific language governing permissions and
<a name="line11"></a>// limitations under the License.
<a name="line12"></a>
<a name="line13"></a>// Copyright 2006 Google Inc. All Rights Reserved.
<a name="line14"></a>
<a name="line15"></a>/**
<a name="line16"></a> * @fileoverview
<a name="line17"></a> * DataSource implementation that uses XMLHttpRequest as transport, with
<a name="line18"></a> * response as serialized JS object (not required to be JSON) that can
<a name="line19"></a> * be evaluated and set to a variable.
<a name="line20"></a> *
<a name="line21"></a> * Response can have unexecutable starting/ending text to prevent inclusion
<a name="line22"></a> * using &lt;script src=&quot;...&quot;&gt;
<a name="line23"></a> *
<a name="line24"></a> */
<a name="line25"></a>
<a name="line26"></a>
<a name="line27"></a>goog.provide(&#39;goog.ds.JsXmlHttpDataSource&#39;);
<a name="line28"></a>
<a name="line29"></a>goog.require(&#39;goog.Uri&#39;);
<a name="line30"></a>goog.require(&#39;goog.ds.DataManager&#39;);
<a name="line31"></a>goog.require(&#39;goog.ds.FastDataNode&#39;);
<a name="line32"></a>goog.require(&#39;goog.ds.LoadState&#39;);
<a name="line33"></a>goog.require(&#39;goog.ds.logger&#39;);
<a name="line34"></a>goog.require(&#39;goog.events&#39;);
<a name="line35"></a>goog.require(&#39;goog.net.EventType&#39;);
<a name="line36"></a>goog.require(&#39;goog.net.XhrIo&#39;);
<a name="line37"></a>
<a name="line38"></a>/**
<a name="line39"></a> * Similar to JsonDataSource, with using XMLHttpRequest for transport
<a name="line40"></a> * Currently requires the result be a JS object that can be evaluated and
<a name="line41"></a> * set to a variable and doesn&#39;t require strict JSON notation.
<a name="line42"></a> *
<a name="line43"></a> * @param {string || goog.Uri} uri URI for the request.
<a name="line44"></a> * @param {string} name Name of the datasource.
<a name="line45"></a> * @param {string} opt_startText Text to expect/strip before JS response.
<a name="line46"></a> * @param {string} opt_endText Text to expect/strip after JS response.
<a name="line47"></a> * @param {boolean} opt_usePost If true, use POST. Defaults to false (GET).
<a name="line48"></a> *
<a name="line49"></a> * @extends {goog.ds.FastDataNode}
<a name="line50"></a> * @constructor
<a name="line51"></a> */
<a name="line52"></a>goog.ds.JsXmlHttpDataSource = function(uri, name, opt_startText, opt_endText,
<a name="line53"></a>                                       opt_usePost) {
<a name="line54"></a>  goog.ds.FastDataNode.call(this, {}, name, null);
<a name="line55"></a>  if (uri) {
<a name="line56"></a>    this.uri_ = new goog.Uri(uri);
<a name="line57"></a>    this.xhr_ = new goog.net.XhrIo();
<a name="line58"></a>    this.usePost_ = !!opt_usePost;
<a name="line59"></a>
<a name="line60"></a>    goog.events.listen(this.xhr_, goog.net.EventType.COMPLETE,
<a name="line61"></a>        this.completed_, false, this);
<a name="line62"></a>  } else {
<a name="line63"></a>    this.uri_ = null;
<a name="line64"></a>  }
<a name="line65"></a>  this.startText_ = opt_startText;
<a name="line66"></a>  this.endText_ = opt_endText;
<a name="line67"></a>};
<a name="line68"></a>goog.inherits(goog.ds.JsXmlHttpDataSource, goog.ds.FastDataNode);
<a name="line69"></a>
<a name="line70"></a>
<a name="line71"></a>/**
<a name="line72"></a> * Delimiter for start of JSON data in response.
<a name="line73"></a> * null = starts at first character of response
<a name="line74"></a> * @type {string|undefined}
<a name="line75"></a> * @private
<a name="line76"></a> */
<a name="line77"></a>goog.ds.JsXmlHttpDataSource.prototype.startText_;
<a name="line78"></a>
<a name="line79"></a>
<a name="line80"></a>/**
<a name="line81"></a> * Delimiter for end of JSON data in response.
<a name="line82"></a> * null = ends at last character of response
<a name="line83"></a> * @type {string|undefined}
<a name="line84"></a> * @private
<a name="line85"></a> */
<a name="line86"></a>goog.ds.JsXmlHttpDataSource.prototype.endText_;
<a name="line87"></a>
<a name="line88"></a>
<a name="line89"></a>/**
<a name="line90"></a> * Gets the state of the backing data for this node
<a name="line91"></a> * @return {goog.ds.LoadState} The state.
<a name="line92"></a> */
<a name="line93"></a>goog.ds.JsXmlHttpDataSource.prototype.getLoadState = function() {
<a name="line94"></a>  return this.loadState_;
<a name="line95"></a>};
<a name="line96"></a>
<a name="line97"></a>
<a name="line98"></a>/**
<a name="line99"></a> * Sets the request data. This can be used if it is required to
<a name="line100"></a> * send a specific body rather than build the body from the query
<a name="line101"></a> * parameters. Only used in POST requests.
<a name="line102"></a> * @param {string} data The data to send in the request body.
<a name="line103"></a> */
<a name="line104"></a>goog.ds.JsXmlHttpDataSource.prototype.setQueryData = function(data) {
<a name="line105"></a>  this.queryData_ = data;
<a name="line106"></a>};
<a name="line107"></a>
<a name="line108"></a>
<a name="line109"></a>/**
<a name="line110"></a> * Load or reload the backing data for this node.
<a name="line111"></a> * Fires the JsonDataSource
<a name="line112"></a> */
<a name="line113"></a>goog.ds.JsXmlHttpDataSource.prototype.load = function() {
<a name="line114"></a>  goog.ds.logger.info(&#39;Sending JS request for DataSource &#39; +
<a name="line115"></a>      this.getDataName() + &#39; to &#39; + this.uri_);
<a name="line116"></a>
<a name="line117"></a>  if (this.uri_) {
<a name="line118"></a>    if (this.usePost_) {
<a name="line119"></a>
<a name="line120"></a>      var queryData;
<a name="line121"></a>      if (!this.queryData_) {
<a name="line122"></a>        queryData = this.uri_.getQueryData().toString();
<a name="line123"></a>      } else {
<a name="line124"></a>        queryData = this.queryData_;
<a name="line125"></a>      }
<a name="line126"></a>
<a name="line127"></a>      var uriNoQuery = this.uri_.clone();
<a name="line128"></a>      uriNoQuery.setQueryData(null);
<a name="line129"></a>      this.xhr_.send(String(uriNoQuery), &#39;POST&#39;, queryData);
<a name="line130"></a>    } else {
<a name="line131"></a>      this.xhr_.send(String(this.uri_));
<a name="line132"></a>    }
<a name="line133"></a>  } else {
<a name="line134"></a>    this.loadState_ = goog.ds.LoadState.NOT_LOADED;
<a name="line135"></a>  }
<a name="line136"></a>};
<a name="line137"></a>
<a name="line138"></a>
<a name="line139"></a>/**
<a name="line140"></a> * Called on successful request.
<a name="line141"></a> * @private
<a name="line142"></a> */
<a name="line143"></a>goog.ds.JsXmlHttpDataSource.prototype.success_ = function()  {
<a name="line144"></a>  goog.ds.DataManager.getInstance().fireDataChange(this.getDataName());
<a name="line145"></a>};
<a name="line146"></a>
<a name="line147"></a>
<a name="line148"></a>/**
<a name="line149"></a> * Completed callback. Loads data if successful, otherwise sets
<a name="line150"></a> * state to FAILED
<a name="line151"></a> * @param {goog.events.Event} e Event object, Xhr is target.
<a name="line152"></a> * @private
<a name="line153"></a> */
<a name="line154"></a>goog.ds.JsXmlHttpDataSource.prototype.completed_ = function(e) {
<a name="line155"></a>  if (this.xhr_.isSuccess()) {
<a name="line156"></a>    goog.ds.logger.info(&#39;Got data for DataSource &#39; + this.getDataName());
<a name="line157"></a>    var text = this.xhr_.getResponseText();
<a name="line158"></a>
<a name="line159"></a>    // Look for start and end token and trim text
<a name="line160"></a>    if (this.startText_) {
<a name="line161"></a>      var startpos = text.indexOf(this.startText_);
<a name="line162"></a>      text = text.substring(startpos + this.startText_.length);
<a name="line163"></a>    }
<a name="line164"></a>    if (this.endText_) {
<a name="line165"></a>      var endpos = text.lastIndexOf(this.endText_);
<a name="line166"></a>      text = text.substring(0, endpos)
<a name="line167"></a>    }
<a name="line168"></a>
<a name="line169"></a>    // Eval result
<a name="line170"></a>    /** @preserveTry */
<a name="line171"></a>    try {
<a name="line172"></a>      var jsonObj = eval(&#39;[&#39; + text + &#39;][0]&#39;);
<a name="line173"></a>      this.extendWith_(jsonObj);
<a name="line174"></a>      this.loadState_ = goog.ds.LoadState.LOADED;
<a name="line175"></a>    }
<a name="line176"></a>    catch (ex) {
<a name="line177"></a>      // Invalid JS
<a name="line178"></a>      this.loadState_ = goog.ds.LoadState.FAILED;
<a name="line179"></a>      goog.ds.logger.severe(&#39;Failed to parse data: &#39; + ex.message);
<a name="line180"></a>    }
<a name="line181"></a>
<a name="line182"></a>    // Call on a timer to avoid threading issues on IE
<a name="line183"></a>    goog.global.setTimeout(goog.bind(this.success_, this), 0)
<a name="line184"></a>  } else {
<a name="line185"></a>    goog.ds.logger.info(&#39;Data retrieve failed for DataSource &#39; +
<a name="line186"></a>        this.getDataName());
<a name="line187"></a>    this.loadState_ = goog.ds.LoadState.FAILED;
<a name="line188"></a>  }
<a name="line189"></a>};
</pre>


</body>
</html>
